package org.ftc.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.List;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {
	
	public static void main(String[] aa ){
		 try {
			 
			List<File> files = Arrays.asList(new File("D:/ziptest/x.txt"),new File("D:/ziptest/y.txt"));
			unzip(files);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static File unzip(List<File> files){
		try {
			File tmp = null;
			if (files != null && files.size() > 0) {
				tmp = File.createTempFile("gstp", ".zip");
				ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tmp));
				for( File file : files){
					zipFile(file, zos );
				}
				zos.finish();
				zos.close();
			}
			return tmp;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
	}
	
	public static void zipFile(File file, ZipOutputStream zos ) throws Exception{
		int l;
		byte[] b = new byte[(int) file.length()];
		FileInputStream fis = new FileInputStream(file);
		ZipEntry entry = new ZipEntry(file.getName());
		zos.putNextEntry(entry);  
		zos.setEncoding("big5");  
		while((l = fis.read(b)) != -1){
			zos.write(b, 0, l);
		}  
		entry = null;
		fis.close();
		b = null;
	}
	
	
}
